package dao

import (
	"Management/common"
	"Management/model"
)
//查询部门
func QueryDepartment(id int)(department model.Department) {
	db:=common.InitDB()
	db.Where("id = ?",id).First(&department)
	return department
}
//查询所有部门
func QueryDepartments()(departments []model.Department) {
	db:=common.InitDB()
	db.Find(&departments)
	return departments
}
//新建部门
func AddDepartment(id_ago int,path string,department_name string,responsibility string,phone string)(err error){
	db:=common.InitDB()
	department:=model.Department{
		IdAgo:id_ago,
		Path:path+"/"+department_name,
		DepartmentName:department_name,
		Responsibility: responsibility,
		Phone: phone,
	}
	err=db.Create(&department).Error
	//保存进入数据库
	return err
}
//删除部门
func DeleteDepartment(id int)(err error){
	db:=common.InitDB()
	var department model.Department
	err=db.Where("id =?",id).Unscoped().Delete(&department).Error
	return err
}
//修改部门
func ChangeDepartment(id int,department_name string,responsibility string,phone string)(err error){
	db:=common.InitDB()
	var department model.Department
	err=db.Where("id =?",id).First(&department).Update("department_name",department_name).Update("responsibility",responsibility).Update("phone",phone).Error
	return err
}